#include "Bullet.h"
#include "../SGD Wrappers/SGD_Geometry.h"
#include "EnemyUnit.h"
#include "GameplayState.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "AnimationManager.h"
#include "DestroyObjectMessage.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"

Bullet::~Bullet()
{
	m_pOwner->Release();
	m_pOwner = nullptr;
}

/*virtual*/ void Bullet::Update(float elapsedTime) /*override*/
{
	MovingObject::Update(elapsedTime);

	if (IsDead())
	{
		DestroyObjectMessage* pMsg = new DestroyObjectMessage{ this };
		pMsg->QueueMessage();
		pMsg = nullptr;
	}
}
///*virtual*/ void Bullet::Render(void) /*override*/
//{
//	
//}
/*virtual*/ void Bullet::HandleCollision(const IBase* pOther)	/*override*/
{
	if (pOther->GetType() == OBJ_PLAYER)
	{
		if (GetOwner() != pOther)
		{
			//Create and send the TANK_HIT event
			SGD::Event* msg = new SGD::Event{ "HIT"};
			msg->QueueEvent((Player*)pOther);
			msg = nullptr;

		//	Send the DestroyObjectMessage for the bullet
			DestroyObjectMessage* dMsg = new DestroyObjectMessage{ this };

			dMsg->QueueMessage();
			dMsg = nullptr;
		}
	}
	
	else if (pOther->GetType() == OBJ_BASE
		|| pOther->GetType() == OBJ_WALL
		|| pOther->GetType() == OBJ_VDESK
		|| pOther->GetType() == OBJ_HDESK)
	{
		//Send the DestroyObjectMessage for the bullet
		DestroyObjectMessage* dMsg = new DestroyObjectMessage{ this };

		dMsg->QueueMessage();
		dMsg = nullptr;
	}

}
void Bullet::SetAnimation(std::string animName)
{
	animation.m_strCurrAnimation = animName;
	animation.m_fCurrDuration = 0.0f;
	animation.m_nCurrFrame = 0;
}


void Bullet::SetOwner(EnemyUnit* _owner)
{
	if (m_pOwner != nullptr)
	{
		m_pOwner->Release();
		m_pOwner = nullptr;
	}

	m_pOwner = _owner;
	m_pOwner->AddRef();
}

bool Bullet::IsDead()
{
	SGD::Rectangle world = { 0, 0,
		GameplayState::GetInstance()->GetWorldSize().width,
		GameplayState::GetInstance()->GetWorldSize().height };

	if (GetRect().IsIntersecting(world))
		return false;
	else
		return true;

}